//============================================================================
// Name        : proxy.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;
#include "http/HttpRequestParser.hpp"
#include <string>
#include "util/Log.hpp"
#include <sstream>

#include "io/EventDispatcher.hpp"
#include "io/Server.hpp"
#include "io/ServerTask.hpp"
#include <signal.h>
#include <stdio.h>
#include "io/TaskThreadPool.hpp"

///TODO: BUG http://vk.com/js/common.js
// Accept-Encoding: identity

int main(int argc, char * argv[]) {

	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
		Log::v("Can't set signal ");
	}
	int port = 5555;

#ifdef MULTITHREAD_VERSION_2
	int threadpoolsize = 1;
	if (argc < 1) {
		Log::e("No args! Using ./proxy threadpoolsize [port] ");
	}
	std::stringstream ss;
	ss << argv[1];
	ss >> threadpoolsize;
	if (argc > 2) {
		std::stringstream ss;
		ss << argv[2];
		ss >> port;
	}
#else
	if (argc > 1) {
		std::stringstream ss;
		ss << argv[1];
		ss >> port;
	}
#endif

	EventDispatcher dispatcher;
	Server server(port);
	server.start();

#ifdef MULTITHREAD_VERSION_2

	TaskThreadPool * pool = new TaskThreadPool(threadpoolsize);
	dispatcher.addTask(new ServerTask(server, pool));
#else
	dispatcher.addTask(new ServerTask(server, NULL));
#endif

	dispatcher.run();

	return 0;
}
